<template>
  <div class="left-control-panel" @click="handlePanelClick">
    <!-- 合约代码显示 -->
    <!-- <div class="contract-code-display">
      {{ currentContract?.code || panelContract?.code || '未选择' }}
    </div> -->

    <div class="contract-code-controls">
      <div class="zoom-controls">
        <button @click="zoomOut" class="zoom-btn" :disabled="fontSize <= 2">
          -
        </button>
        <button @click="zoomIn" class="zoom-btn" :disabled="fontSize >= 26">
          +
        </button>
      </div>
      <div class="zoom-level-display">{{ fontSize + 4 }}</div>
    </div>

    <div class="current-time" :class="{ flashing: isFlashing }">{{ currentTime }}</div>

    <!-- 价格变化信息 -->
    <div class="price-info-section">
      <div class="price-change-display">
        <div
          class="price-change-value"
          :class="{
            negative: priceChangePercent.startsWith('-'),
            positive:
              !priceChangePercent.startsWith('-') && priceChangePercent !== '',
          }"
        >
          {{ priceChangePercent || "0.00" }}%
        </div>
      </div>

      <div class="market-stats">
        <!-- 总成交量 -->
        <div class="stat-item">{{ totalVolume || 0 }}</div>
        <!-- 总持仓量 -->
        <div class="stat-item">{{ totalPosition || 0 }}</div>
        <!-- 持仓变化 -->
        <div class="stat-item">{{ dailyPositionChange || 0 }}</div>
      </div>
    </div>

    <!-- 订单类型选择 -->
    <div class="order-type-group">
      <label class="radio-label">
        <input
          type="radio"
          :checked="orderType === 'A'"
          @change="$emit('update:orderType', 'A')"
          value="A"
        />
        <span class="radio-text">单点位</span>
      </label>
      <label class="radio-label">
        <input
          type="radio"
          :checked="orderType === 'B'"
          @change="$emit('update:orderType', 'B')"
          value="B"
        />
        <span class="radio-text">多点位</span>
      </label>
    </div>

    <!-- 交易选项 -->
    <div class="order-options">
      <label class="radio-label">
        <input
          type="radio"
          name="combo-option"
          :checked="options.noCombo"
          @click="handleRadioToggle('noCombo')"
        />
        <span class="radio-text">防连点</span>
      </label>
      <!-- <label class="checkbox-label">
        <input type="checkbox" :checked="options.autoHand" @change="handleOptionChange('autoHand', $event)" />
        <span class="checkbox-text">金</span>
      </label> -->
      <div class="checkbox-group">
        <label class="radio-label">
          <input
            type="radio"
            name="limit-option"
            :checked="options.cLimit499"
            @click="handleRadioToggle('cLimit499')"
          />
          <span class="radio-text">499</span>
        </label>
        <label class="radio-label">
          <input
            type="radio"
            name="limit-option"
            :checked="options.noLimit"
            @click="handleRadioToggle('noLimit')"
          />
          <span class="radio-text">2000</span>
        </label>
      </div>

      
      <!-- <label class="checkbox-label">
        <input type="checkbox" :checked="options.upLimit" @change="handleOptionChange('upLimit', $event)" />
        <span class="checkbox-text">UpLimit</span>
      </label> -->
    </div>

    <!-- 下单数量输入 -->
    <div class="order-inputs">
      <div class="input-group">
        <span class="input-label">轻：</span>
        <input
          :value="lightOrderQuantity"
          @input="handleLightQuantityChange"
          type="number"
          class="order-input"
          min="1"
          max="30"
        />
      </div>
      <div class="input-group">
        <span class="input-label">重：</span>
        <input
          :value="heavyOrderQuantity"
          @input="handleHeavyQuantityChange"
          type="number"
          class="order-input"
          min="2"
        />
      </div>
    </div>

    <!-- 个人持仓量显示 -->
    <div class="zero-values">
      <div class="zero-value red">{{ redValue || 0 }}</div>
      <div class="zero-value blue">{{ blueValue || 0 }}</div>
    </div>

    <!-- 持仓信息 -->
    <div class="position-info-section">
      <div class="position-line">
        <div>
          <span class="label">操作次数：</span>
          <span>{{ cPosition || 0 }}</span>
        </div>
        <div>
          <span class="label">成交量：</span>
          <span>{{ tPosition || 0 }}</span>
        </div>
      </div>
      <div class="position-line jing-cang">
        <div class="label">C</div>
        <div class="value">{{ netPosition || 0 }}</div>
      </div>
    </div>

    <!-- 测试按钮 -->
    <!-- <div class="test-buttons">
      <button class="test-btn" @click="handleTestStrategy">测试策略</button>
    </div> -->

  </div>
</template>

<script setup lang="ts">
import type { ContractInfo } from "@/types/trading";

// Props
const props = defineProps<{
  currentContract?: ContractInfo | null;
  panelContract?: ContractInfo | null;
  fontSize: number;
  currentTime: string;
  isFlashing: boolean;
  priceChangePercent: string;
  totalVolume: number;
  totalPosition: number;
  dailyPositionChange: number;
  redValue: number;
  blueValue: number;
  lightOrderQuantity: number;
  heavyOrderQuantity: number;
  orderType: string;
  options: {
    autoHand: boolean;
    cLimit499: boolean;
    noLimit: boolean;
    noCombo: boolean;
    upLimit: boolean;
  };
  netPosition: number;
  cPosition: number;
  tPosition: number;
  hasActiveOrders: boolean;
}>();

// Emits
const emit = defineEmits<{
  zoomIn: [];
  zoomOut: [];
  "update:lightOrderQuantity": [value: number];
  "update:heavyOrderQuantity": [value: number];
  "update:orderType": [value: string];
  "update:options": [value: any];
  "cancel-all-orders": [];
  "cancel-all-account-orders": [];
  "buy-long": [];
  "sell-short": [];
  "cancel-order": [];
  "cancel-all": [];
  "test-position": [];
  "test-strategy": [];
  "panel-click": [event: MouseEvent];
}>();

// Methods
const zoomIn = () => {
  emit("zoomIn");
};

const zoomOut = () => {
  emit("zoomOut");
};

// 处理输入变化
const handleLightQuantityChange = (event: Event) => {
  const target = event.target as HTMLInputElement;
  emit("update:lightOrderQuantity", Number(target.value));
};

const handleHeavyQuantityChange = (event: Event) => {
  const target = event.target as HTMLInputElement;
  emit("update:heavyOrderQuantity", Number(target.value));
};

// 处理选项更新
const updateOption = (key: string, value: boolean) => {
  const newOptions = { ...props.options };

  // 499和无限互斥逻辑
  if (key === "cLimit499" && value) {
    newOptions.cLimit499 = true;
    newOptions.noLimit = false;
  } else if (key === "noLimit" && value) {
    newOptions.noLimit = true;
    newOptions.cLimit499 = false;
  } else {
    newOptions[key] = value;
  }

  emit("update:options", newOptions);
};

const handleOptionChange = (key: string, event: Event) => {
  const target = event.target as HTMLInputElement;
  updateOption(key, target.checked);
};

// 处理单选按钮的切换逻辑（支持取消选中）
const handleRadioToggle = (key: string) => {
  const currentValue = props.options[key];
  
  // 如果当前已选中，则取消选中；如果未选中，则选中
  if (key === 'cLimit499' || key === 'noLimit') {
    // 对于互斥的选项组，如果点击已选中的项，则取消选中
    if (currentValue) {
      updateOption(key, false);
    } else {
      updateOption(key, true);
    }
  } else {
    // 对于独立选项，直接切换状态
    updateOption(key, !currentValue);
  }
};

// 处理测试策略按钮点击
const handleTestStrategy = () => {
  emit("test-strategy");
};

// 处理面板点击事件
const handlePanelClick = (event: MouseEvent) => {
  const target = event.target as HTMLElement;

  // 检查点击的是否为输入框、按钮或其他交互元素
  const isInteractiveElement =
    target.tagName === "INPUT" ||
    target.tagName === "BUTTON" ||
    target.closest("button") ||
    target.closest("input") ||
    target.closest(".zoom-btn") ||
    target.closest(".trading-btn") ||
    target.closest(".cancel-account-btn") ||
    target.closest(".test-position-btn") ||
    target.closest(".checkbox-label");

  // 如果点击的不是交互元素，则发送面板点击事件
  if (!isInteractiveElement) {
    emit("panel-click", event);
  }
};
</script>

<style scoped lang="less">
/* 左侧操作列 */
.left-control-panel {
  width: 80px;
  min-width: 80px;
  /* 移除最大宽度限制，允许在窗口缩放时适应 */
  background: #b7b4b9;
  border: 1px inset #c0c0c0;
  display: flex;
  flex-direction: column;
  gap: 3px;
  flex-shrink: 0;
  box-sizing: border-box;
}

.contract-code-controls {
  display: flex;
  justify-content: space-between;
  padding: 8px 4px 0 4px;
  align-items: center;
}

/* 合约代码显示 */
.contract-code-display {
  background: #ffffff;
  border: 1px inset #c0c0c0;
  padding: 2px 4px;
  text-align: center;
  font-weight: bold;
  font-size: 10px;
  color: #000000;
}

/* 缩放控制 */
.zoom-controls {
  display: flex;
  gap: 5px;
  justify-content: center;
}

/* 当前时间显示 */
.current-time {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  color: #000;
  border-radius: 3px;
  padding: 3px 0;
  letter-spacing: 1px;
  font-weight: bold;
  transition: all 0.3s ease;
  // min-width: 80px;
}

/* 收盘前闪烁效果 */
.current-time.flashing {
  animation: flash-warning 0.5s infinite;
}

@keyframes flash-warning {
  0%, 100% {
    color: #ff0000;
    background-color: rgba(255, 0, 0, 0.1);
  }
  50% {
    color: #000;
    background-color: transparent;
  }
}

.zoom-btn {
  width: 16px;
  height: 16px;
  border: none;
  background: #fff;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
  color: #000000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.zoom-btn:active {
  border: 1px inset #fff;
}

.zoom-btn:disabled {
  background: #fff;
  color: #999999;
  cursor: not-allowed;
  opacity: 0.8;
}

.zoom-level-display {
  font-size: 16px;
  font-weight: bold;
  color: #000000;
  padding: 1px 4px;
  text-align: center;
  min-width: 20px;
  cursor: help;
}

/* 价格信息区域 */
.price-info-section {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.price-change-display {
  padding: 2px;
  text-align: center;
}

.price-change-value {
  font-size: 20px;
  font-weight: bold;
}

.price-change-value.negative {
  color: #ff0000;
}

.price-change-value.positive {
  color: #008000;
  font-size: 20px;
}

/* 市场统计数据 */
.market-stats {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.stat-item {
  padding: 1px 2px;
  text-align: center;
  font-size: 20px;
  color: #000000;
  font-weight: bold;
}

/* 红蓝数值显示 */
.zero-values {
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 0 4px;
}

.zero-value {
  height: 18px;
  line-height: 18px;
  text-align: center;
  color: white;
  font-size: 12px;
  font-weight: bold;
  border: 1px inset #c0c0c0;
}

.zero-value.red {
  background: #cf3e20ff;
}

.zero-value.blue {
  background: #289472ff;
}

/* 下单输入框 */
.order-inputs {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 0 8px;
  border: none;
  margin-bottom: 3px;
  margin-top: 10px;
}

.input-group {
  display: flex;
  vertical-align: middle;
  align-items: center;

  .input-label {
    font-size: 12px;
    color: #000000;
    font-weight: bold;
  }
}

.order-input {
  width: 30px;
  height: 18px;
  border: none;
  background: #ffffff;
  text-align: center;
  font-size: 10px;
  color: #000000;
  border-radius: 2px;
}

.order-input:focus {
  outline: none;
  border: none;
}

/* 去掉input number类型的上下箭头 */
.order-input::-webkit-outer-spin-button,
.order-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Firefox浏览器去掉上下箭头 */
.order-input[type="number"] {
  -moz-appearance: textfield;
  appearance: textfield;
}

/* 订单类型选择 */
.order-type-group {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.radio-label {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #000000;
  cursor: pointer;
  font-weight: blod;
  line-height: 20px;
  padding-left: 2px;
}

.radio-label input[type="radio"] {
  margin-right: 4px;
  width: 14px;
  height: 14px;
  vertical-align: middle;
}

.radio-text {
  user-select: none;
  font-weight: bold;
}

/* 交易选项 */
.order-options {
  display: flex;
  flex-direction: column;
  gap: 1px;
}


/* 持仓信息 */
.position-info-section {
  margin-top: 5px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4px;
}

.position-line {
  font-size: 11px;
  color: #000000;
  text-align: left;
  width: 100%;
  font-weight: bold;


  &.jing-cang {
    // height: 40px;
    font-size: 35px;
    text-align: center;
    margin-top: 10px;
    font-weight: bold;
    font-family: '';
      color: rgb(65, 103, 209);


    .label {
      color: #000;
    }
  }
}

/* 交易按钮 */
.trading-buttons {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 3px 0;
}

/* 盈亏显示 */
.pnl-display {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  background: #ffffff;
  border: 1px inset #c0c0c0;
  padding: 3px;
  margin-top: auto;
}

.pnl-value {
  font-size: 12px;
  font-weight: bold;
  color: #000000;
}

.pnl-letter {
  font-size: 14px;
  font-weight: bold;
  color: #000000;
}

/* 滚动条样式 */
.left-control-panel::-webkit-scrollbar {
  width: 5px;
}

.left-control-panel::-webkit-scrollbar-track {
  background: #c0c0c0;
  border-radius: 4px;
}

.left-control-panel::-webkit-scrollbar-thumb {
  background: #808080;
  border-radius: 4px;
}

.left-control-panel::-webkit-scrollbar-thumb:hover {
  background: #606060;
}

/* 测试按钮样式 */
.test-buttons {
  padding: 2px;
  display: flex;
  justify-content: center;
}

.test-btn {
  background: #e0e0e0;
  border: 1px outset #c0c0c0;
  padding: 2px 6px;
  font-size: 10px;
  cursor: pointer;
  border-radius: 2px;
  color: #333;

  &:hover {
    background: #f0f0f0;
  }

  &:active {
    border: 1px inset #c0c0c0;
    background: #d0d0d0;
  }
}
</style>
